package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.exception.CustomException;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import com.itheima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper scm;

    /**
     * 删减购物车里面数量
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart sub(ShoppingCart shoppingCart) {
        //判断携带过来的参数是否是菜品
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        if (shoppingCart.getDishId() != null) {
            //不等于null，就说明是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            //否者是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //追加查询条件，判断是那个用户过来进行操作的；
        lqw.eq(ShoppingCart::getUserId, BaseContext.get());
        //先找到这个商品||套餐
        ShoppingCart sc = scm.selectOne(lqw);
        //因为是删减，所有数据库里肯定有这条数据；不需要判断是否是第一次来；
        //重新设置商品数量，在原来的基础上-1；
        sc.setNumber(sc.getNumber() - 1);
        scm.updateById(sc);
         if (sc.getNumber() == 0) {
            scm.deleteById(sc.getId());
        }
        //因为删减数量会减成负数，这里做一个判断，数量小于0，抛出异常
        if (sc.getNumber() < 0) {
            throw new CustomException("删除数量有误请,正确删除！");
        }
        return sc;
    }

    /**
     * 添加到购物车
     *
     * @param shoppingCart
     * @return
     */
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //先判断添加的是套餐还是菜品
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();

        if (dishId != null) {
            //如果不等于null，说明添加的是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //追加用户id表明要查询这个用户的购物车数据里面的这件菜品|套餐的数据
        lqw.eq(ShoppingCart::getUserId, BaseContext.get());
        ShoppingCart sc = scm.selectOne(lqw);
        if (sc != null) {
            //表明这件菜品|套餐在数据库表里面有记录，修改数量..
            sc.setNumber(sc.getNumber() + 1);
            //更新数据库的数据
            scm.updateById(sc);
            return sc;
        } else {
            shoppingCart.setNumber(1);                       //如果是第一次，设置商品数量为1
            shoppingCart.setUserId(BaseContext.get());       //添加用户id
            shoppingCart.setCreateTime(LocalDateTime.now()); //添加订单创建时间
            scm.insert(shoppingCart);                        //新建这条数据到数据库
            return shoppingCart;
        }


    }

    /**
     * 购物车展示
     *
     * @return
     */
    @Override
    public List<ShoppingCart> selectList() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //根据用户id查询所有购物车数据；
        lqw.eq(ShoppingCart::getUserId, BaseContext.get());
        return scm.selectList(lqw);
    }

    /**
     * 清空购物车
     */
    @Override
    public int deleteAll() {
        //直接删除数据库里面的所有数据
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.get());
        int row = scm.delete(lqw);
        return row;
    }
}
